package com.yand.thinkingInJava.thinkingInJava21.ShareResource;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * i ++ 递增操作不是一个原子行操作
 */
public class AtomicityTest implements Runnable{
    private int i = 0;
    public int getValue(){
        return  i;
    }


    private synchronized void eventIncrement(){
        i ++;
        i ++;
    }
    @Override
    public void run() {
       while(true){
           eventIncrement();
       }
    }


    public static void main(String [] args){
        ExecutorService exec = Executors.newCachedThreadPool();
        AtomicityTest at = new AtomicityTest();

        exec.execute(at);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while(true){
            int val = at.getValue();
            System.out.println(val);
            if(val %2 != 0){
                System.out.println(val);
                System.exit(0);
            }
        }


    }


}
